package app.framework.controller;

import app.biz.config.MongoDataSourceConfig;
import app.framework.component.Sequencer;
import app.framework.controller.base.BaseController;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.mongodb.client.model.Filters.eq;

/**
 * 用于存活检测、基准性能测试等
 * */
@RestController
public class MyController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(MyController.class);
    @Autowired  Sequencer sequencer;

    @GetMapping(value = "/public/health")
    public String health() throws AuthenticationException{
        return "I'm OK!";
    }

    @GetMapping(value = "/bizlog/reduce")
    public void reduceBizlog(@RequestParam  long sid) throws AuthenticationException{
        removeContentFromBizlog(sid);
    }

    @Autowired
    MongoDataSourceConfig mongo;

    private void removeContentFromBizlog(long sid) {
        Document filter = new Document().append("sid", sid);
        Document content = new Document().append("content", "cleardByresult");
        Document update = new Document("$set",content);
        mongo.getCollecton("bizlog").updateOne(filter, update);
    }

    @GetMapping(value = "/public/helloworld")
    public String helloworld() throws AuthenticationException{
        return "hello world";
    }

    @GetMapping(value = "/public/helloworlds")
    public String helloworlds() throws AuthenticationException{
        return "hello world, "+sequencer.getId();
    }
}
